﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// 泛型 + 委托 + 事件
namespace CS_BEGINNER_TO_PROFICIENT.STD.DelegatedSTD
{
    public delegate void WeatherChangedHandler<T>(object sender, T e);
    
    public class WeatherStation<T>
    {
        public event WeatherChangedHandler<T> WeatherChanged;

        private T _weather; 

        public void ChangeWeather(T newWeather)
        {
            _weather = newWeather;
            RaiseWeatherChanged(this, newWeather);
        }


        public void RaiseWeatherChanged(object sender, T e)
        {
            WeatherChanged?.Invoke(sender, e);
        }
    }

    public class WeatherApp<T>
    {
        public WeatherApp(WeatherStation<T> station)
        {
            station.WeatherChanged += OnWeatherChanged;
        }

        public void OnWeatherChanged(object sender, T e) => Console.WriteLine($"The Weather Changed to {e}");
    }

    public class DelegatedSTDP14
    {
       public static void Execute()
        {
            WeatherStation<string> station = new WeatherStation<string>();
            WeatherApp<string> app = new WeatherApp<string>(station);

            station.ChangeWeather("Sun");
        }
    }
}
